package com.nosweattechnet.server;

import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;

import com.nosweattechnet.datatypes.Place;

public class LocationSearchAsyncTask extends CommunicatorAsyncTask {
	private BasicNameValuePair tag=new BasicNameValuePair("tag","radiusSearch");
	private String radiusString="radius";
	private String latitudeString="latitude";
	private String longitudeString="longitude";
	
	//Creates a new Async task and adds the associated name value pairs
	public LocationSearchAsyncTask(Float r,Float lat,Float lon){
		super();
		super.addNameValuePair(tag);
		super.addNameValuePair(new BasicNameValuePair(radiusString,r.toString()));
		super.addNameValuePair(new BasicNameValuePair(latitudeString,lat.toString()));
		super.addNameValuePair(new BasicNameValuePair(longitudeString,lon.toString()));
	}
	
	//Parses the JSONArray results returned from the database after a search query and returns
	//an ArrayList of Places
	public ArrayList<Place> getPlaces() throws InterruptedException, ExecutionException{
		ArrayList<Place> ai=new ArrayList<Place>();
		JSONArray jarr=this.get();
		if(jarr==null){
			return ai;
		}
		for(int i=0;i<jarr.length();i++){
			try {
				ai.add(new Place(jarr.getJSONObject(i)));
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return ai;
	}
}
